package com.haixiaoke.saas.lock.service.impl;

import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;

import cn.hutool.core.util.StrUtil;
import cn.hutool.extra.qrcode.QrCodeUtil;
import cn.hutool.extra.qrcode.QrConfig;
import com.alibaba.fastjson2.JSONObject;
import com.aliyun.oss.model.PutObjectRequest;
import com.haixiaoke.saas.common.exception.ServiceException;
import com.haixiaoke.saas.common.oss.AliOssServiceImpl;
import com.haixiaoke.saas.common.oss.OssService;
import com.haixiaoke.saas.common.utils.DateUtils;
import com.haixiaoke.saas.lock.domain.LockQrCode;
import com.haixiaoke.saas.lock.mapper.LockQrCodeMapper;
import com.haixiaoke.saas.lock.service.ILockQrCodeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.imageio.ImageIO;

/**
 * 设备二维码Service业务层处理
 *
 * @author panda
 * @date 2025-05-15
 */
@Service
public class LockQrCodeServiceImpl implements ILockQrCodeService {
    @Autowired
    private LockQrCodeMapper lockQrCodeMapper;
    @Autowired
    private OssService ossService;

    /**
     * 查询设备二维码
     *
     * @param qrCodeId 设备二维码主键
     * @return 设备二维码
     */
    @Override
    public LockQrCode selectLockQrCodeByQrCodeId(Long qrCodeId) {
        return lockQrCodeMapper.selectLockQrCodeByQrCodeId(qrCodeId);
    }

    /**
     * 查询设备二维码列表
     *
     * @param lockQrCode 设备二维码
     * @return 设备二维码
     */
    @Override
    public List<LockQrCode> selectLockQrCodeList(LockQrCode lockQrCode) {
        return lockQrCodeMapper.selectLockQrCodeList(lockQrCode);
    }

    /**
     * 新增设备二维码
     *
     * @param lockQrCode 设备二维码
     * @return 结果
     */
    @Override
    public int insertLockQrCode(LockQrCode lockQrCode) {
        if (StrUtil.isEmpty(lockQrCode.getLockMac())) {
            throw new ServiceException("lockMac不能为空");
        }
        int count = lockQrCodeMapper.countLockQrCodeByMac(lockQrCode.getLockMac());
        if (count > 0) {
            throw new ServiceException("设备二维码已存在");
        }
        ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
        BufferedImage generate = QrCodeUtil.generate(JSONObject.toJSONString(lockQrCode), new QrConfig());
        try {
            ImageIO.write(generate, "jpg", byteOut);

            // 将输出流内容转为字节数组
            byte[] imageBytes = byteOut.toByteArray();

            // 将字节数组转为InputStream
            InputStream inputStream = new ByteArrayInputStream(imageBytes);

            // 上传文件
            String url = ossService.upload("deviceslock", "jpg", lockQrCode.getLockMac()+".jpg", imageBytes.length, inputStream);
            lockQrCode.setQrCodeImage(url);
        } catch (IOException e) {
            throw new ServiceException(e.getMessage());
        }
        lockQrCode.setCreateTime(DateUtils.getNowDate());
        return lockQrCodeMapper.insertLockQrCode(lockQrCode);
    }

    /**
     * 修改设备二维码
     *
     * @param lockQrCode 设备二维码
     * @return 结果
     */
    @Override
    public int updateLockQrCode(LockQrCode lockQrCode) {
        lockQrCode.setUpdateTime(DateUtils.getNowDate());
        return lockQrCodeMapper.updateLockQrCode(lockQrCode);
    }

    /**
     * 批量删除设备二维码
     *
     * @param qrCodeIds 需要删除的设备二维码主键
     * @return 结果
     */
    @Override
    public int deleteLockQrCodeByQrCodeIds(Long[] qrCodeIds) {
        return lockQrCodeMapper.deleteLockQrCodeByQrCodeIds(qrCodeIds);
    }

    /**
     * 删除设备二维码信息
     *
     * @param qrCodeId 设备二维码主键
     * @return 结果
     */
    @Override
    public int deleteLockQrCodeByQrCodeId(Long qrCodeId) {
        return lockQrCodeMapper.deleteLockQrCodeByQrCodeId(qrCodeId);
    }
}
